package com.ruoyi.project.jiaqin.companyrecruit.controller;

import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.recruitindustry.domain.RecruitIndustry;
import com.ruoyi.project.jiaqin.recruitindustry.service.IRecruitIndustryService;
import com.ruoyi.project.jiaqin.region.domain.Region;
import com.ruoyi.project.jiaqin.region.service.IRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companyrecruit.domain.CompanyRecruit;
import com.ruoyi.project.jiaqin.companyrecruit.service.ICompanyRecruitService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业招聘Controller
 *
 * @author admin
 * @date 2021-01-13
 */
@Controller
@RequestMapping("/jiaqin/companyrecruit")
public class CompanyRecruitController extends BaseController
{
    private String prefix = "jiaqin/companyrecruit";

    @Autowired
    private ICompanyRecruitService companyRecruitService;
    @Autowired
    private ICompanyService iCompanyService;
    @Autowired
    private IRecruitIndustryService iRecruitIndustryService;
    @Autowired
    private IRegionService iRegionService;

    @RequiresPermissions("jiaqin:companyrecruit:view")
    @GetMapping()
    public String companyrecruit()
    {
        return prefix + "/companyrecruit";
    }

    /**
     * 查询企业招聘列表
     */
    @RequiresPermissions("jiaqin:companyrecruit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyRecruit companyRecruit)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null) {
            companyRecruit.setCompanyId(company.getId());
            startPage();
            List<CompanyRecruit> list = companyRecruitService.selectCompanyRecruitList(companyRecruit);
            return getDataTable(list);
        }

        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyRecruit> list = companyRecruitService.selectCompanyRecruitList(companyRecruit);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出企业招聘列表
     */
    @RequiresPermissions("jiaqin:companyrecruit:export")
    @Log(title = "企业招聘", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyRecruit companyRecruit)
    {
        List<CompanyRecruit> list = companyRecruitService.selectCompanyRecruitList(companyRecruit);
        ExcelUtil<CompanyRecruit> util = new ExcelUtil<CompanyRecruit>(CompanyRecruit.class);
        return util.exportExcel(list, "companyrecruit");
    }

    /**
     * 新增企业招聘
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        //查询省
        Region region = new Region();
        region.setType(1);
        List<Region> provinces = iRegionService.selectRegionList(region);
        //查询行业
        List<RecruitIndustry> recruitIndustries = iRecruitIndustryService.selectRecruitIndustryList(new RecruitIndustry());
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        mmap.put("recruitIndustries",recruitIndustries);
        mmap.put("provinces",provinces);
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }
        return prefix + "/add";
    }

    /**
     * 新增保存企业招聘
     */
    @RequiresPermissions("jiaqin:companyrecruit:add")
    @Log(title = "企业招聘", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyRecruit companyRecruit)
    {
        return toAjax(companyRecruitService.insertCompanyRecruit(companyRecruit));
    }

    /**
     * 修改企业招聘
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        //查询行业
        List<RecruitIndustry> recruitIndustries = iRecruitIndustryService.selectRecruitIndustryList(new RecruitIndustry());
        CompanyRecruit companyRecruit = companyRecruitService.selectCompanyRecruitById(id);
        Region region = new Region();
        region.setType(1);
        List<Region> provinces = iRegionService.selectRegionList(region);
        mmap.put("recruitIndustries",recruitIndustries);
        mmap.put("provinces",provinces);
        mmap.put("companyRecruit", companyRecruit);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业招聘
     */
    @RequiresPermissions("jiaqin:companyrecruit:edit")
    @Log(title = "企业招聘", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyRecruit companyRecruit)
    {
        return toAjax(companyRecruitService.updateCompanyRecruit(companyRecruit));
    }

    /**
     * 删除企业招聘
     */
    @RequiresPermissions("jiaqin:companyrecruit:remove")
    @Log(title = "企业招聘", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyRecruitService.deleteCompanyRecruitByIds(ids));
    }
}
